﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using Services.BusinessRules;
namespace Services
{

    public partial class BranchService
    {
       readonly IBranchEntity entity;
       readonly IAuditEntity AuditEntity;
       public BranchService()
       {
           this.entity = new BranchEntity();
           this.AuditEntity = new AuditEntity();
       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model, ref string mess)
       {
           try
           {
               if (Validate((Branch)_model, ref mess))
               {
                   var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
                   if (id == 0)
                   {
                       return entity.Save((CoreData.Branch)_model, Table.Branch.ToString());
                   }
                   else
                   {
                       return entity.Update((CoreData.Branch)_model, Table.Branch.ToString());
                   }
               }
               else {
                   return -1;
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Branch GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.Branch.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Branch> GetList()
       {
           try
           {
              // return entity.GetAll(Table.Branch.ToString()).ToList();
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllBranch," Branch1.IsDeleted = 'false'")).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Branch> GetListByLINQ(Func<CoreData.Branch, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Branch.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Branch GetOneByLINQ(Func<CoreData.Branch, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Branch.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Branch> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               if (_searchstring.Contains("ParentID"))
                   return entity.GetBySearchString(_searchstring, Table.Branch.ToString()).ToList();
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllBranch, " Branch1.IsDeleted = 'false' and "+_searchstring)).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Branch)_model, Table.Branch.ToString());

               //Audit audit = new Audit();
               //audit.ModifiedDate = DateTime.Now;
               //audit.UserID = ValueDefine.UserID;
               //audit.Module = "Ngành học";
               //audit.Code = "Branch";
               //audit.Action = "Xóa";
               //audit.Description = " Xóa ngành: " + obj.Name;
               //AuditEntity.Save(audit, Table.Audit.ToString());

               return true;
           }
           catch { return false; }
       }


       /// <summary>
       /// Delete a object by hidden
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool UpdateIsDelete(object _model)
       {
           try
           {
               var obj = (CoreData.Branch)_model;
               obj.IsDeleted = true;
               entity.Update(obj, Table.Branch.ToString());

               Audit audit = new Audit();
               audit.ModifiedDate = DateTime.Now;
               audit.UserID = ValueDefine.UserID;
               audit.Module = "Ngành học";
               audit.Code = "Branch";
               audit.Action = "Xóa";
               audit.Description = " Xóa ngành: " + obj.Name;
               AuditEntity.Save(audit, Table.Audit.ToString());


               return true;
           }
           catch { return false; }
       }

       #endregion



        #region Other Method

       public Branch GetBranchByClassID(long classiD)
       {
           try {
               var data = entity.GetByCusTomSQL(String.Format(SQLCommand.GetBranchByClass, " Class.ID =" + classiD)).FirstOrDefault();
               return data;
           }
           catch { return null; }
       }


       public Branch GetBranchByParentID(long parentid)
       {
           try
           {
               var data = entity.GetBySearchString(" ParentID=0 and ID=" + parentid, Table.Branch.ToString()).FirstOrDefault();
               return data;
           }
           catch { return null; }
       }


        public Branch GetByCode(string Code)
       {
           try
           {
               var data = entity.GetBySearchString(" Code='"+Code+"'", Table.Branch.ToString()).FirstOrDefault();
               return data;
           }
           catch { return null; }
        }

        public Branch GetByName(string Name)
        {
            try
            {
                var data = entity.GetBySearchString(" Name='" + Name + "'", Table.Branch.ToString()).FirstOrDefault();
                return data;
            }
            catch { return null; }
        }


       bool Validate(Branch obj, ref string mess)
       {

           new ValidateRequired("Mã ngành", "Code", obj.Code);
           new ValidateRequired("Tên ngành", "Name", obj.Name);
        //   new ValidateExist("Mã", "Code", "Code", obj, GetList().Cast<object>().ToList());
         //  new ValidateExist("Tên", "Name", "Name", obj, GetList().Cast<object>().ToList());
           mess = BusinessObject.Validate();

           if (mess == null)
           {

               var code = GetByCode(obj.Code);
               if (code != null && (obj.ID != code.ID))
               {
                   mess = "Mã ngành bị trùng";
               }
           }
           if (mess == null)
           {

               var code = GetByName(obj.Name);
               if (code != null && (obj.ID != code.ID))
               {
                   mess = "Tên ngành bị trùng";
               }
           }

           BusinessObject.Clear();
           return mess == null ? true : false;
       }


       string Diff(Branch OldItem, Branch NewItem)
       {
           string content = "";
           if (NewItem.ID == 0)
           {
               content = "Thêm mới một ngành học: " + NewItem.Name;
           }
           else
           {
               if (OldItem.Code != NewItem.Code)
               {
                   content += "- Mã: " + OldItem.Code + " --> " + NewItem.Code + "\r\n";
               }
               if (OldItem.Name != NewItem.Name)
               {
                   content += "- Tên: " + OldItem.Name + " --> " + NewItem.Name + "\r\n";
               }
               if (OldItem.Description != NewItem.Description)
               {
                   content += "- Ghi chú: " + OldItem.Description + " --> " + NewItem.Description + "\r\n";
               }
               if (OldItem.Active != NewItem.Active)
               {
                   content += "- Active: " + OldItem.Active + " --> " + NewItem.Active + "\r\n";
               }
           }
           return content;
       }



        #endregion 
    
    }
         
}
